CUDA 内核开发始于对一个 内核的定义,它是一个专门设计用于在拥有海量核心数的 NVIDIA GPU上并行执行的特殊 C++ 函数。这些函数是 CUDA 编程模型中的基本工作单元,充当了串行主机逻辑向大规模并行设备执行过渡的桥梁。
1. __global__ 限定符
该 __global__ 声明限定符是必需的 API 限定符,它指示编译器为 GPU 生成代码,同时保持函数入口点对 CPU 可见。 可在主机上调用并在 GPU 上执行的函数被称为内核。
2. 执行环境
内核被分派到并执行于 流式多处理器(SMs)。SM 是 NVIDIA GPU 中的核心计算引擎,负责管理数百个并发线程。每个 SM 负责处理线程块,并将它们调度到处理核心上。
语法规则: 内核必须严格返回 void。由于它们与主机异步运行,无法直接向 CPU 返回值;必须将结果写回分配的设备内存中。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary function of the
__global__ specifier?It defines a function that runs on the CPU but is callable from the GPU.
It defines a kernel that runs on the GPU and is callable from the CPU.
It allocates memory on the GPU's SM cache.
It synchronizes all threads in a block.
✅ Correct!
Correct! __global__ is the bridge used to launch kernels from Host code.❌ Incorrect
Incorrect. __global__ specifically identifies entry-point kernels for GPU execution called by the Host.QUESTION 2
Why must CUDA kernels return
void?Because they execute asynchronously and have no direct path to return values to the Host thread.
To save registers on the SM.
Because GPU memory is read-only.
The NVCC compiler does not support float returns.
✅ Correct!
Exactly. Since kernels launch asynchronously, the Host doesn't wait for a return value; results must be written to pointers.❌ Incorrect
The return value restriction is due to the asynchronous nature of GPU execution and Host-Device separation.QUESTION 3
Which hardware component is responsible for managing and executing threads in a CUDA kernel?
The PCIe Controller.
The Streaming Multiprocessor (SM).
The Host RAM controller.
The BIOS.
✅ Correct!
Yes, the SM is the core unit on the GPU where threads are scheduled and executed.❌ Incorrect
The SM (Streaming Multiprocessor) is the heart of GPU compute hardware.QUESTION 4
What happens when a Host calls a kernel function?
The CPU halts until the GPU finish processing.
The GPU creates a clone of the function for every available SM.
The kernel is enqueued for execution on the GPU, and the CPU continues to the next instruction.
The CPU performs a context switch to the GPU.
✅ Correct!
Correct. Kernel launches are non-blocking (asynchronous) from the Host's perspective.❌ Incorrect
CUDA kernels are asynchronous; the CPU does not wait unless explicitly told to do so via synchronization.QUESTION 5
Which of the following is the correct definition of a CUDA kernel?
A function that executes on the GPU and is invoked from the Host.
A C++ library for file I/O.
A hardware driver for NVIDIA GPUs.
A standard CPU function with the __gpu__ prefix.
✅ Correct!
Perfect. This is the fundamental definition of a kernel in CUDA programming.❌ Incorrect
A kernel is specifically the code designed to run on the GPU while being launched from the Host.Module Challenge: Designing a Vector Subtraction Kernel
Applying kernel fundamentals to data transformation.
You are tasked with porting a signal processing routine to the GPU. The core operation subtracts background noise (Vector B) from a signal (Vector A) into a result (Vector C).
Q
1. Write the function signature for a kernel named 'vecSub' that takes three float pointers.
Solution:
__global__ void vecSub(float* A, float* B, float* C)Q
2. In which hardware unit will the logic inside your 'vecSub' kernel physically reside during execution?
Solution:
The logic will be executed on the Streaming Multiprocessors (SMs) of the NVIDIA GPU.
The logic will be executed on the Streaming Multiprocessors (SMs) of the NVIDIA GPU.
Q
3. If you attempt to return a float status code from this kernel, why will the compiler throw an error?
Solution:
CUDA Kernels must have a
CUDA Kernels must have a
void return type because they are executed asynchronously. Any status reporting or data return must be performed through device memory pointers.